package com.siqi.hr.system.common.utils;

import com.siqi.hr.system.common.utils.intf.BaseEnum;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 处理角色可登陆模块工具类
 * @author     xiaogq
 * Created by xiaogoingqin on 2016/7/22.
 */
public class EnumUtils {

    /**
     * 将角色可登陆模块字符串转换成集合格式(xiaogq)
     * @param source
     * @param K
     * @param <T>
     * @return
     */
    public static <T extends BaseEnum> Collection<T> tranform(String source, Class<T> K) {
        Collection o = new ArrayList();
        char[] cs = source.toCharArray();
        for (int i = 0; i < cs.length; i++) {
            if (cs[i] == '1'){
                o.add(getByCode(Integer.valueOf(i + 1), K));
            }
        }
        return o;
    }

    /**
     * 获取可登陆模块的编号
     * @param code
     * @param enumClass
     * @param <T>
     * @return
     */
    public static <T extends BaseEnum> T getByCode(Object code, Class<? extends BaseEnum> enumClass) {
        return (T) getByCode(code, (BaseEnum[]) enumClass.getEnumConstants());
    }

    /**
     * 获取可登陆模块的编号
     * @param code
     * @param <T>
     * @return
     */
    public static <T extends BaseEnum> T getByCode(Object code, T[] values) {
        if (code == null)
            return null;
        for (BaseEnum item : values) {
            if (item.getCode().equals(code)) {
                return (T) item;
            }
        }
        return null;
    }

}
